<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('通知模板') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('公告编码')" prop="noticeCode">
        <el-input
          v-model="queryParams.noticeCode"
          :placeholder="$t('公告编码')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('公告类型')" prop="noticeType">
        <el-input
          v-model="queryParams.noticeType"
          :placeholder="$t('公告类型')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('通知类型')" prop="noticeMode">
        <el-select v-model="queryParams.noticeMode" clearable :placeholder="$t('请选择')">
          <el-option
            v-for="(dict, index) in noticeModeOptions"
            :key="index"
            :value="dict.dictValue"
            :label="$t(dict.dictLabel)"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:msgtemplate:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="msgTemplateList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('公告编码')" align="center" prop="noticeCode" />
      <el-table-column :label="$t('公告类型')" align="center" prop="noticeType">
        <template slot-scope="scope">
          <span>
            {{ $t(scope.row.noticeType) }}
          </span>
        </template>
      </el-table-column>
      <!-- <el-table-column :label="$t('备注')" align="center" prop="remark" /> -->
      <el-table-column :label="$t('默认发送用户')" align="center" prop="defaultSendUser" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-for="(ele, index) in scope.row.defaultSendUserList" :key="index">
            {{ ele.nickName }}{{ index === scope.row.defaultSendUserList.length - 1 ? '' : ',' }}
          </span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('通知方式')" align="center" prop="noticeMode" :formatter="noticeModeFormat">
        <template slot-scope="scope">
          <span v-for="(ele, index) in noticeModeFormat(scope.row)" :key="index">
            {{ ele }}{{ index === noticeModeFormat(scope.row).length - 1 ? '' : ',' }}
          </span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:msgtemplate:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['system:msgtemplate:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改通知模板对话框 -->
    <el-dialog :title="$t('通知模板')" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <msgTemplateForm ref="msgTemplateForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listMsgTemplate, delMsgTemplate, exportMsgTemplate } from '@/api/system/message/msgTemplate'
import msgTemplateForm from './msgTemplateForm.vue'
export default {
    components: {
        msgTemplateForm
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 通知模板表格数据
          msgTemplateList: [],
          // 是否显示弹出层
          formOpen: false,
        // 通知方式字典
        noticeModeOptions: [],
          // 查询参数
          queryParams: {
              noticeCode: undefined,
              noticeType: undefined,
              noticeMode: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {
        noticeCode: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], noticeType: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], noticeMode: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ] }
      }
  },
  created() {
      this.getList()
      this.getDicts('sys_notice_type').then(response => {
          this.noticeModeOptions = response.data
      })
  },
  methods: {
    /** 查询通知模板列表 */
    getList() {
        this.loading = true
        listMsgTemplate(this.queryParams).then(response => {
            this.msgTemplateList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 通知方式字典翻译
    noticeModeFormat(row) {
      const arry = []
      row.noticeMode.forEach((e) => {
        arry.push(this.selectDictLabel(this.noticeModeOptions, e))
      })
      return arry
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            noticeCode: undefined,
            noticeType: undefined,
            remark: undefined,
            defaultSendUser: undefined,
            defaultSendUserList: undefined,
            noticeMode: undefined,
            systemNoticeTitle: undefined,
            systemNoticeContent: undefined,
            mailNoticeTitle: undefined,
            mailNoticeContent: undefined,
            appNoticeTitle: undefined,
            appNoticeContent: undefined,
            smsNoticeCode: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.msgTemplateForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.msgTemplateForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delMsgTemplate(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        const queryParams = this.queryParams
        this.$confirm(this.$t('是否确认导出?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return exportMsgTemplate(queryParams)
        }).then(response => {
            this.download(response.msg)
        }).catch(function() {
        })
    }
  }
}
</script>
